package com.cw.base.jeeyc.biz.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.base.common.model.util.CacheKeyUtil;
import com.cw.base.jeeyc.biz.converter.DeployDictItemConverter;
import com.cw.base.jeeyc.biz.intf.DeployDictItemBiz;
import com.cw.base.jeeyc.dao.mapper.DeployDictItemMapper;
import com.cw.base.jeeyc.dao.model.DeployDictItemDo;
import com.cw.base.jeeyc.deploy.dto.DeployDictItemDto;
import com.cw.lang.cache.Cache;

/**
 * 数据字典项接口业务实现类
 *
 * @author chenw
 * @date 2019-08-26 21:21:57
 */
@Service
public class DeployDictItemBizImpl extends ServiceImpl<DeployDictItemMapper, DeployDictItemDo>
    implements DeployDictItemBiz {
    @Autowired
    private DeployDictItemMapper deployDictItemMapper;
    @Autowired
    private Cache<String, DeployDictItemDto> cache;

    /**
     * 新增数据字典项
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addDeployDictItem(DeployDictItemDto request) {
        if (request == null) {
            return null;
        }
        deployDictItemMapper.insert(DeployDictItemConverter.dto2Do(request));
        return "";
    }

    /**
     * 更新数据字典项
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyDeployDictItem(DeployDictItemDto request) {
        if (request == null) {
            return false;
        }
        deployDictItemMapper.updateById(DeployDictItemConverter.dto2Do(request));
        return true;
    }

    /**
     * 删除数据字典项
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDeployDictItem(DeployDictItemDto request) {
        if (request == null) {
            return false;
        }
        deployDictItemMapper.deleteById(DeployDictItemDo.builder()
            .dictKey(request.getDictKey())
            .itemKey(request.getItemKey())
            .build());
        String cacheKey = CacheKeyUtil.getDictCacheKey(request.getDictKey());
        cache.delete(cacheKey);
        return true;
    }
}
